//TODO: Please clean this section up, there is a bunch of functions and definitions that should be located elsewhere


try{
	
gpsFormUpdates = false;
setInterval("_mapctrlLooper()",100);
//setInterval("_radioLooper()",500);
	
$(function()
{	
	$("#gohome").click(function() {gmGoHome();});
	$("#lockonplane").click(function(){gmLockOnPlane();});
	$("#gotobug").click(function(){gmGotoBug();});
	$("#gotoplane").click(function(){gmGotoPlane();});
	$("#hdgbug").click(function(){gmToggleHeadingBug();});
	$("#goto").buttonset();
	$("#moveBugToPlane").click(function(){gmMoveBugToPlane();});
	$("#moveBugToGS").click(function(){gmMoveBugToGS();});
	$("#battery").progressbar({value: 0,disabled:false});
	$("#wireless_strength_gs").progressbar({value: 0,disabled:false});
	$("#wireless_strength_p").progressbar({value: 0,disabled:false});
	$("#signalpage").click(function(){
		$("#signal-form").dialog("open");
		$("#more-form").dialog("close");
	});
	$("#setgsloc").click(function(){
		$("#gs-form").dialog("open");
		$("#lat").val(gs_lat);
		$("#lon").val(gs_lon);
	});
	$("#maxMinMapCtrl").click(function(){
		$("#map_canvas").toggle("blind", {}, 500);
	});
	function updateTips(t) {
		$(".validateTips")
			.text(t)
			.addClass("ui-state-highlight");
		setTimeout(function(){
			tips.removeClass("ui-state-highlight", 1500);
		}, 500 );
	}
	function checkRegexp(o, regexp, n)
	{
		if (regexp.test(o.val())){return false;}
		o.addClass("ui-state-error");
		updateTips(n);
		return false;
	}
	$("#more").click(function(){
		//alert("YO");
		$("#more-form").dialog("open");
	});
	$("#more-form").dialog({
		closeOnEscape: true,
		autoOpen: false,
		width: 300,
		height: 400,
		modal: true,
		buttons: {
			Cancel: function() {
				$(this).dialog("close");
			}
		}
	});
	$("#gpsinfo").click(function(){
		gpsFormUpdates = true;
		$("#more-form").dialog("close");
		$("#gps-form").dialog("open");
	});
	$("#autotruckerpage").click(function(){
		$("#autotrucker-form").dialog("open");
		$("#more-form").dialog("close");
	});
	
	$("#gps-form").dialog({
		closeOnEscape: true,
		autoOpen: false,
		height: 500,
		width: 450,
		modal: false,
		buttons: {
			Close: function() {
				$(this).dialog("close");
			}
		},
		close: function () {
			gpsFormUpdates = false;
		}
	});
	
	$("#autotrucker-form").dialog({
		closeOnEscape: true,
		autoOpen: false,
		height: 600,
		width: 500,
		model: false,
		buttons: {
			Close: function() {
				$(this).dialog("close");
			}
		}
	})
	
	$("#gs-form").dialog({
		closeOnEscape: true,
		autoOpen: false,
		height: 400,
		width: 500,
		modal: true,
		buttons: {
			"Set": function() {
				try
				{
					var bValid = true;
					$("#lat").removeClass("ui-state-error");
					$("#lon").removeClass("ui-state-error");
					//bValid = bValid && checkRegexp( $("#lat"), /^([0-9.-])+$/, "Latitude field contains unreccognised charecters" );
					//bValid = bValid && checkRegexp( $("#lon"), /^([0-9.-])+$/, "Longitude field contains unreccognised charecters" );
					
					if (bValid)
					{
						gmChangeGSpos(parseFloat($("#lat").val()), parseFloat($("#lon").val()));
						$(this).dialog("close");
					}
					else
					{
						alert("Not valid!");
					}
				}catch(e){alert(e);}
			},
			"Plane": function(){
				try
				{
					if (plane_lat == null || plane_lon == null)
					{
						updateTips("GPS on plane does not have lock");
					}
					else
					{
						gmChangeGSpos(plane_lat,plane_lon);
						$(this).dialog("close");
					}
				}catch(e){alert(e);}
			},
			"View": function(){
				try
				{
					gmChangeGSpos(map.getCenter().lat(),map.getCenter().lng());
					$(this).dialog("close");
				}
				catch(e){alert(e);}
			},
			Cancel: function() {
				$(this).dialog("close");
			}
		},
		close: function() {
			$("#lat").val("").removeClass("ui-state-error");
			$("#lon").val("").removeClass("ui-state-error");
		}
	});
	$("#checklistspage").click(function(){
		$("#checklist-form").dialog("open");
		$("#more-form").dialog("close");
	});
	$("#open-before-startup-clst").click(function(){
		$("#before-startup-clst-form").dialog("open");
		$("#checklist-form").dialog("close");
	});
	$("#open-startup-clst").click(function(){
		$("#startup-clst-form").dialog("open");
		$("#checklist-form").dialog("close");
	});
	$("#open-preflight-clst").click(function(){
		$("#preflight-clst-form").dialog("open");
		$("#checklist-form").dialog("close");
	});
	$("#before-startup-clst-form").dialog({
		width: 600,
		height: 800,
		buttons: {
			"Startup (next)": function() {
				$("#startup-clst-form").dialog("open");
				$(this).dialog("close");
			},
			"Checklists": function() {
				$("#checklist-form").dialog("open");
				$(this).dialog("close");
			},
			"Close": function(){$(this).dialog("close");}
		}
	});
	$("#startup-clst-form").dialog({
		width: 700,
		height: 800,
		buttons: {
			"Pre-Flight (next)": function() {
				$("#preflight-clst-form").dialog("open");
				$(this).dialog("close");
			},
			"Checklists": function() {
				$("#checklist-form").dialog("open");
				$(this).dialog("close");
			},
			"Close": function(){$(this).dialog("close");}
		}
	});
	$("#preflight-clst-form").dialog({
		width: 500,
		height: 800,
		buttons: {
			"Checklists": function() {
				$("#checklist-form").dialog("open");
				$(this).dialog("close");
			},
			"Close": function(){$(this).dialog("close");}
		}
	});
});
function lockReleased()
{
	$("#lockonplane").animate({color: "#FFFFFF" }, 500);
}
function claimLock()
{
	$("#lockonplane").animate({color: "#00FF00" }, 500);
}

function bugVisible()
{
	$("#hdgbug").animate({color: "#7777FF" }, 500);
}
function bugHidden()
{
	$("#hdgbug").animate({color: "#FFFFFF" }, 500);
}
function setBattery(per)
{
	$("#battery").setValue(per);
}

function _mapctrlLooper()
{
	try{
	if (gpsFormUpdates)
	{
		$(document).ready(function(){
			$.ajax({
				type: "GET",
				url: "http://localhost:8080/at_gps",
				dataType: "xml",
				success: function(xml) {
					gps_status = $(xml).find("status").text();
					gps_lat = $(xml).find("lat").text();
					gps_lon = $(xml).find("lon").text();
					gps_alt = $(xml).find("alt").text();
					gps_speed = $(xml).find("speed").text();
					gps_hdop = $(xml).find("hdop").text();
					gps_vdop = $(xml).find("vdop").text();
					gps_pdop = $(xml).find("pdop").text();
					gps_course = $(xml).find("course").text();
					gps_deviation = $(xml).find("deviation").text();
					gps_tracking = $(xml).find("activeSats").text();
					gps_vsat = new Array();
					$(xml).find("vsats").find("vsat").each(function(){
						var prn,elev,azi,snr;
						prn = parseInt($(this).find("prn").text());
						elev = parseInt($(this).find("elev").text());
						azi = parseInt($(this).find("azi").text());
						snr = parseInt($(this).find("snr").text());
						gps_vsat.push({'prn':prn,'elev':elev,'azi':azi,'snr':snr});
					});
					gps_vsat_count = gps_vsat.length;
				}
			});//AJAX
		});//DOC READY
		if (gps_status=="0")
		{
			$("#gps-valid").val("D/C");
			$("#gps-lat").val("");
			$("#gps-lon").val("");
			$("#gps-alt").val("");
			$("#gps-spd").val("");
			$("#gps-course").val("");
			$("#gps-vs").val("");
			$("#gps-pdop").val("");
			$("#gps-vdop").val("");
			$("#gps-hdop").val("");
			$("#gps-tracking").val("");
		}
		else
		{
			if (gps_status=="1"){$("#gps-valid").val("NO FIX");}
			else if (gps_status=="2"){$("#gps-valid").val("GOOD");}
			$("#gps-lat").val(gps_lat);
			$("#gps-lon").val(gps_lon);
			if (plane_alt_method=="GPS")
			{$("#gps-alt").val(gps_alt);}
			else{$("#gps-alt").val("");}
			if (plane_speed_method=="GPS")
			{$("#gps-spd").val(Math.floor(gps_speed*10)/10);}
			else{$("#gps-spd").val("");}
			$("#gps-course").val(Math.floor(gps_course));
			$("#gps-vs").val(gps_vsat_count);
			$("#gps-tracking").val(gps_tracking);
			$("#gps-pdop").val(gps_pdop);
			$("#gps-vdop").val(gps_vdop);
			$("#gps-hdop").val(gps_hdop);
			drawSat();
		}
	}
	}
	catch (e)
	{
		alert(e);
	}
}

function _radioLooper()
{
	try{
	$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "http://localhost:8080/radios",
		dataType: "xml",
		success: function(xml) {
			$(xml).find("gs").each(function(){
				var signal = $(this).find("signal").text();
				$("#gs_signal").val(signal);
				gs_signal = Math.abs(signal);
				$("#wireless_strength_gs").progressbar( "value" , gs_signal );
				$("gs_gotit").val($(this).find("gotit").text());
			});
		}
	});
	});
	}catch(e){alert(e);}
}

}//end try
catch (e)
{
	alert(e);
}
